package com.pureut.system.service.impl;

import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.domain.SysVerification;
import com.pureut.system.domain.SysVerificationDetailed;
import com.pureut.system.mapper.SysVerificationMapper;
import com.pureut.system.service.ISysVerificationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 编码规则实现类
 * @Param
 * @return 结果
 * @Date 22-11-22
 * @Author hl
 **/
@Service
public class SysVerificationServiceImpl implements ISysVerificationService {


    @Resource
    private SysVerificationMapper sysVerificationMapper;


    /**
     * 查询编码规则
     */
    @Override
    public List<SysVerification> selectVerificationList(SysVerification sysVerification) {


        // 配置类型（数据字典）
        List<SysDictData> netArray = DictUtils.getDictCache("sys_configuration_type");
        Map<String, String> netMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        List<SysVerification> list = sysVerificationMapper.selectVerificationList(sysVerification);
        for (SysVerification dto : list){
            // 获取数据字典标签
            dto.setVerTypeDict(netMap.get(String.valueOf(dto.getVerType())));
        }

        for (SysVerification ch : list){
            long verId = ch.getVerId();
            ch.setSysVerificationDetailedList(sysVerificationMapper.selectVerificationDetailed(verId));
        }

        return list;
    }

    /**
     * 根据id查询编码校验
     * @param verId
     * @return
     */
    @Override
    public List<SysVerification> selectVerificationById(long verId) {

        List<SysVerification> list = sysVerificationMapper.selectVerificationById(verId);

        for (SysVerification ch : list){
            ch.setSysVerificationDetailedList(sysVerificationMapper.selectVerificationDetailed(verId));
        }
        return list;
    }

    /**
     * 新增编码校验
     * @param sysVerification
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVerification(SysVerification sysVerification) throws Exception{

        // 判断规则编码是否唯一
        SysVerification Unique = sysVerificationMapper.selectVerificationUnique(sysVerification.getVerTheRules());
        if (Unique != null) {
            throw new GlobalException("这个规则编码已存在!");
        }

        sysVerification.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        sysVerification.setCreateTime(new Date());
        List<String> list = sysVerification.getVerDocumentsCodes();
        String str = "";
        for (String s: list) {
            str +=s+",";
        }
        str=str.substring(0,str.length()-1);
        sysVerification.setVerDocumentsCodes1(str);
        // 新增编码
        int cn = sysVerificationMapper.insertVerification(sysVerification);
        if(cn==0){
            throw new SQLException("抛出异常");
        }
        // 编码规则id
        long verId = sysVerification.getVerId();
//        JSONObject json = sysVerification.getVerConfigCode();
//        JSONArray arr = json.getJSONArray("detailed");
        // 编码校验明细JSON
//        List<SysVerificationDetailed> list = JSONArray.parseArray(arr.toJSONString(), SysVerificationDetailed.class);

        List<SysVerificationDetailed> sysVerificationDetailedList = sysVerification.getSysVerificationDetailedList();

        for (SysVerificationDetailed sv:
                sysVerificationDetailedList) {
            sv.setDetVerificationId(verId);
        }
        // 新增编码校验明细数据
        cn = sysVerificationMapper.insertVerificationDetailed(sysVerificationDetailedList);
        return cn;
    }

    /**
     * 修改编码校验
     * @param sysVerification
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVerification(SysVerification sysVerification) throws Exception {

        sysVerification.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        sysVerification.setUpdateTime(new Date());
        int cn = sysVerificationMapper.updateVerification(sysVerification);
        if(cn==0){
            throw new SQLException("抛出异常");
        }

        // 编码规则id
        long verId = sysVerification.getVerId();

        // 删除编码校验明细
        int deDetailed = sysVerificationMapper.deleteVerificationDetailedById(verId);
        if (deDetailed==0){
            throw new SQLException("抛出异常");
        }
//        JSONObject json = sysVerification.getVerConfigCode();
//        JSONArray arr = json.getJSONArray("detailed");
//        // 编码规则明细JSON
//        List<SysVerificationDetailed> list = JSONArray.parseArray(arr.toJSONString(), SysVerificationDetailed.class);
        List<SysVerificationDetailed> sysVerificationDetailedList = sysVerification.getSysVerificationDetailedList();

        for (SysVerificationDetailed sv:
                sysVerificationDetailedList) {
            sv.setDetVerificationId(verId);
        }

//        for (SysVerificationDetailed sv:
//                list) {
//            sv.setDetVerificationId(verId);
//        }
        // 新增编码规则明细数据
        cn = sysVerificationMapper.insertVerificationDetailed(sysVerificationDetailedList);
        return cn;
    }

    /**
     * 删除编码规则
     */
    @Override
    public int deleteVerificationById(String verId) {
        String[] verIdS = verId.split(",");
        return sysVerificationMapper.deleteVerificationById(verIdS);
    }


}
